package com.byb.server.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.byb.server.service.EnsureScopeService;
import com.byb.server.web.rest.util.HeaderUtil;
import com.byb.server.web.rest.util.PaginationUtil;
import com.byb.server.service.dto.EnsureScopeDTO;
import io.swagger.annotations.ApiParam;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing EnsureScope.
 */
@RestController
@RequestMapping("/api")
public class EnsureScopeResource {

    private final Logger log = LoggerFactory.getLogger(EnsureScopeResource.class);

    private static final String ENTITY_NAME = "ensureScope";

    private final EnsureScopeService ensureScopeService;

    public EnsureScopeResource(EnsureScopeService ensureScopeService) {
        this.ensureScopeService = ensureScopeService;
    }

    /**
     * POST  /ensure-scopes : Create a new ensureScope.
     *
     * @param ensureScopeDTO the ensureScopeDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new ensureScopeDTO, or with status 400 (Bad Request) if the ensureScope has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/ensure-scopes")
    @Timed
    public ResponseEntity<EnsureScopeDTO> createEnsureScope(@RequestBody EnsureScopeDTO ensureScopeDTO) throws URISyntaxException {
        log.debug("REST request to save EnsureScope : {}", ensureScopeDTO);
        if (ensureScopeDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new ensureScope cannot already have an ID")).body(null);
        }
        EnsureScopeDTO result = ensureScopeService.save(ensureScopeDTO);
        return ResponseEntity.created(new URI("/api/ensure-scopes/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /ensure-scopes : Updates an existing ensureScope.
     *
     * @param ensureScopeDTO the ensureScopeDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated ensureScopeDTO,
     * or with status 400 (Bad Request) if the ensureScopeDTO is not valid,
     * or with status 500 (Internal Server Error) if the ensureScopeDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/ensure-scopes")
    @Timed
    public ResponseEntity<EnsureScopeDTO> updateEnsureScope(@RequestBody EnsureScopeDTO ensureScopeDTO) throws URISyntaxException {
        log.debug("REST request to update EnsureScope : {}", ensureScopeDTO);
        if (ensureScopeDTO.getId() == null) {
            return createEnsureScope(ensureScopeDTO);
        }
        EnsureScopeDTO result = ensureScopeService.save(ensureScopeDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, ensureScopeDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /ensure-scopes : get all the ensureScopes.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of ensureScopes in body
     */
    @GetMapping("/ensure-scopes")
    @Timed
    public ResponseEntity<List<EnsureScopeDTO>> getAllEnsureScopes(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of EnsureScopes");
        Page<EnsureScopeDTO> page = ensureScopeService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/ensure-scopes");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /ensure-scopes/:id : get the "id" ensureScope.
     *
     * @param id the id of the ensureScopeDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the ensureScopeDTO, or with status 404 (Not Found)
     */
    @GetMapping("/ensure-scopes/{id}")
    @Timed
    public ResponseEntity<EnsureScopeDTO> getEnsureScope(@PathVariable Long id) {
        log.debug("REST request to get EnsureScope : {}", id);
        EnsureScopeDTO ensureScopeDTO = ensureScopeService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(ensureScopeDTO));
    }

    /**
     * DELETE  /ensure-scopes/:id : delete the "id" ensureScope.
     *
     * @param id the id of the ensureScopeDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/ensure-scopes/{id}")
    @Timed
    public ResponseEntity<Void> deleteEnsureScope(@PathVariable Long id) {
        log.debug("REST request to delete EnsureScope : {}", id);
        ensureScopeService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    /**
     * SEARCH  /_search/ensure-scopes?query=:query : search for the ensureScope corresponding
     * to the query.
     *
     * @param query the query of the ensureScope search
     * @param pageable the pagination information
     * @return the result of the search
     */
    @GetMapping("/_search/ensure-scopes")
    @Timed
    public ResponseEntity<List<EnsureScopeDTO>> searchEnsureScopes(@RequestParam String query, @ApiParam Pageable pageable) {
        log.debug("REST request to search for a page of EnsureScopes for query {}", query);
        Page<EnsureScopeDTO> page = ensureScopeService.search(query, pageable);
        HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/ensure-scopes");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

}
